package com.itheima.thread.threadPool.scheduledExecutorService;

import com.itheima.thread.threadPool.executorService.MyRunnable;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-15 20:57
 **/
public class ScheduledAtFixedRate {
    public static void main(String[] args) throws InterruptedException {
        //1:获取一个具备延迟执行任务的线程池对象
        ScheduledExecutorService es = Executors.newScheduledThreadPool(3, new ThreadFactory() {
            int n = 1;
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"自定义线程名:"+n++);
            }
        });
        //2:创建多个任务对象,提交任务,每个任务延迟2秒执
        for (int i = 1; i < 11; i++) {
            es.scheduleWithFixedDelay(new MyRunnable(i),1,2, TimeUnit.SECONDS);
//            es.scheduleAtFixedRate(new MyRunnable(i),1,2, TimeUnit.SECONDS);
        }
        System.out.println("over");
        Thread.sleep(10000);
//        这玩意你不手动停止，会一直打印，因为本身就是周期打印的功能
        es.shutdown();
    }
}
